package org.biubiu0002.baiblog.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: biubiu0002
 * @Date: 2022-05-09 23:52
 **/
public class ThreadLocalCache {

    private static ThreadLocal<Map<String,Object>> threadLocal = ThreadLocal.withInitial(HashMap::new);

    public static void put(String key,Object value){
        threadLocal.get().put(key,value);
    }

    public static Map<String,Object> getMap(){
        return threadLocal.get();
    }

    public static void setMap(Map<String,Object> map){
        threadLocal.set(map);
    }

    public static <T> T get(String key,Object defaultValue){
        return (T) threadLocal.get().getOrDefault(key,defaultValue);
    }

    public static void remove(String key){
        threadLocal.get().remove(key);
    }
}
